package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.SearchFriendsAdapter;
import com.keyancompany.zhongbaopingtai.api.FriendsAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.listener.IClickCancleListener;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.CircleModel;
import com.keyancompany.zhongbaopingtai.model.FriendModel;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;
import com.keyancompany.zhongbaopingtai.weight.IListView;

import java.util.List;

import butterknife.Bind;
import butterknife.BindString;

public class SearchFriendActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    public static final String KEY_WORD = "KEY_WORD";

    @BindString(R.string.search_friends_text)
    String title;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.filter_edit)
    CustomEditText filterEdit;

    @Bind(R.id.country_lvcountry)
    IListView listView;

    @Bind(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;

    @Bind(R.id.empty_view)
    LinearLayout emptyView;

    @Bind(R.id.empty_hint)
    TextView emptyHint;


    private SearchFriendsAdapter mAdapter;

    private String keyWord;

    private int total;

    private int current;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_search_friend;
    }

    @Override
    protected int getMenuId() {
        return R.menu.search_friend_menu;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (TextUtils.isEmpty(filterEdit.getText()))
                    return false;
                keyWord = filterEdit.getText().toString();
                onRefresh();
                return false;
            }
        });
    }

    @Override
    protected void setUpViews() {
        initSwipeLayout(swipeLayout);
        keyWord = getIntent().getStringExtra(KEY_WORD);
        filterEdit.setText(keyWord);
        mAdapter = new SearchFriendsAdapter(mContext, new IClickCancleListener() {
            @Override
            public void clickCancle(int position) {
                FriendsAPI.addFriennds(mAdapter.getItem(position).getPID_(), new IAsyncHttpResponseHandler() {
                    @Override
                    public void start(int type) {

                    }

                    @Override
                    public void success(int type, String json) {
                        BaseModel base = validateJson(json);
                        if (base != null) {
                            T.showShort(mContext, base.msg);
                        }
                    }

                    @Override
                    public void failed(String error) {

                    }

                    @Override
                    public void finish(int type) {

                    }
                }, getLoading(-1));
            }
        });
        listView.setAdapter(mAdapter);
        listView.setStateListener(new IListView.SwipeLayoutState() {
            @Override
            public boolean isRefresh() {
                return swipeLayout.isRefreshing();
            }

            @Override
            public boolean isLastPage() {
                return total == current;
            }

            @Override
            public void requestNextPage() {
                requestTask(++current);
            }
        });
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));
        swipeLayout.setOnRefreshListener(this);
    }

    private void requestTask(int i) {
        FriendsAPI.searchFriends(i, keyWord, this, null);
    }


    @Override
    public void onRefresh() {
        current = 1;
        requestTask(current);
    }


    @Override
    public void success(int type, String json) {
        super.success(type, json);
        swipeLayout.setRefreshing(false);
        BaseModel base = validateJson(json);
        if (base == null) return;
        if (type == FriendsAPI.SEARCH_FRIENDS) {
            JSONObject data = JSONObject.parseObject(base.data);
            total = data.getInteger("total");
            current = data.getInteger("current");


            if (current == 1) {
                mAdapter.clear();
            }
            listView.loadMoreFinish();
            if (total == current) {
                listView.removeFooterView();
            }

            List<FriendModel> friends = JSONArray.parseArray(data.getString("list"), FriendModel.class);
            if (friends != null && !friends.isEmpty()) {
                mAdapter.addAllAfter(friends);
            } else {
                T.showShort(this, base.msg);
            }
            isEmpty();
        }

    }

    @Override
    public void failed(String error) {
        super.failed(error);
        swipeLayout.setRefreshing(false);
    }

    public void isEmpty() {
        if (mAdapter != null && mAdapter.getCount() < 1) {
            filterEdit.setVisibility(View.GONE);
            swipeLayout.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
            emptyHint.setText(R.string.no_friends_result_text);
        } else {
            filterEdit.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        }
    }

}
